namespace Asmex.FileViewer
{
    using System;
    using System.IO;

    public class MModule
    {
        private MDBlobHeap _blobHeap;
        private Asmex.FileViewer.Relocations _fix;
        private MDGUIDHeap _GUIDHeap;
        private ImpExports _imp;
        private Asmex.FileViewer.ModHeaders _mh;
        private MDStringHeap _stringHeap;
        private Asmex.FileViewer.MDTables _tables;
        private MDBlobHeap _USHeap;

        public MModule(BinaryReader reader)
        {
            try
            {
                this._mh = new Asmex.FileViewer.ModHeaders(reader);
            }
            catch (Exception)
            {
                return;
            }
            try
            {
                this._imp = new ImpExports(reader, this);
            }
            catch (Exception)
            {
                return;
            }
            try
            {
                this._fix = new Asmex.FileViewer.Relocations(reader, this);
            }
            catch (Exception)
            {
                return;
            }
            try
            {
                this._stringHeap = new MDStringHeap(reader, this._mh.MetaDataHeaders.StringStreamHeader.Offset + this._mh.MetaDataHeaders.StorageSigAndHeader.Start, (long) this._mh.MetaDataHeaders.StringStreamHeader.Size, this._mh.MetaDataHeaders.StringStreamHeader.Name);
                this._blobHeap = new MDBlobHeap(reader, this._mh.MetaDataHeaders.BlobStreamHeader.Offset + this._mh.MetaDataHeaders.StorageSigAndHeader.Start, (long) this._mh.MetaDataHeaders.BlobStreamHeader.Size, this._mh.MetaDataHeaders.BlobStreamHeader.Name);
                this._GUIDHeap = new MDGUIDHeap(reader, this._mh.MetaDataHeaders.GUIDStreamHeader.Offset + this._mh.MetaDataHeaders.StorageSigAndHeader.Start, (long) this._mh.MetaDataHeaders.GUIDStreamHeader.Size, this._mh.MetaDataHeaders.GUIDStreamHeader.Name);
                if (this._mh.MetaDataHeaders.USStreamHeader != null)
                {
                    this._USHeap = new MDBlobHeap(reader, this._mh.MetaDataHeaders.USStreamHeader.Offset + this._mh.MetaDataHeaders.StorageSigAndHeader.Start, (long) this._mh.MetaDataHeaders.USStreamHeader.Size, this._mh.MetaDataHeaders.USStreamHeader.Name);
                }
            }
            catch (Exception)
            {
                return;
            }
            try
            {
                reader.BaseStream.Position = this._mh.MetaDataTableHeader.End;
                this._tables = new Asmex.FileViewer.MDTables(reader, this);
            }
            catch (Exception)
            {
            }
        }

        public static int DecodeInt32(BinaryReader reader)
        {
            int num = reader.ReadByte();
            if ((num & 0x80) == 0)
            {
                return num;
            }
            if ((num & 0xc0) == 0x80)
            {
                return (((num & 0x3f) << 8) | reader.ReadByte());
            }
            return (((((num & 0x3f) << 0x18) | (reader.ReadByte() << 0x10)) | (reader.ReadByte() << 8)) | reader.ReadByte());
        }

        public string StringFromRVA(BinaryReader reader, uint rva)
        {
            string str;
            long position = reader.BaseStream.Position;
            try
            {
                str = "";
                uint num2 = this.ModHeaders.Rva2Offset(rva);
                reader.BaseStream.Position = num2;
                for (byte i = reader.ReadByte(); i != 0; i = reader.ReadByte())
                {
                    str = str + ((char) i);
                }
            }
            catch (Exception)
            {
                str = "Unable to find string";
            }
            finally
            {
                reader.BaseStream.Position = position;
            }
            return str;
        }

        public MDBlobHeap BlobHeap
        {
            get
            {
                return this._blobHeap;
            }
        }

        public MDGUIDHeap GUIDHeap
        {
            get
            {
                return this._GUIDHeap;
            }
        }

        public ImpExports ImportExport
        {
            get
            {
                return this._imp;
            }
        }

        public Asmex.FileViewer.MDTables MDTables
        {
            get
            {
                return this._tables;
            }
        }

        public Asmex.FileViewer.ModHeaders ModHeaders
        {
            get
            {
                return this._mh;
            }
        }

        public Asmex.FileViewer.Relocations Relocations
        {
            get
            {
                return this._fix;
            }
        }

        public MDStringHeap StringHeap
        {
            get
            {
                return this._stringHeap;
            }
        }

        public MDBlobHeap USHeap
        {
            get
            {
                return this._USHeap;
            }
        }
    }
}
